home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / handyscrollingsample / handysample.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  15.8 KB  |  516 lines

  1. /*
  2.     File: HandySample.c
  3.     
  4.     Description:
  5.         This file contains the main application program for the HandySample.
  6.     Routines in this file are responsible for handling events directed
  7.     at the application.
  8.  
  9.     Copyright:
  10.         © Copyright 1999 Apple Computer, Inc. All rights reserved.
  11.     
  12.     Disclaimer:
  13.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  14.         ("Apple") in consideration of your agreement to the following terms, and your
  15.         use, installation, modification or redistribution of this Apple software
  16.         constitutes acceptance of these terms.  If you do not agree with these terms,
  17.         please do not use, install, modify or redistribute this Apple software.
  18.  
  19.         In consideration of your agreement to abide by the following terms, and subject
  20.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  21.         copyrights in this original Apple software (the "Apple Software"), to use,
  22.         reproduce, modify and redistribute the Apple Software, with or without
  23.         modifications, in source and/or binary forms; provided that if you redistribute
  24.         the Apple Software in its entirety and without modifications, you must retain
  25.         this notice and the following text and disclaimers in all such redistributions of
  26.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  27.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  28.         Apple Software without specific prior written permission from Apple.  Except as
  29.         expressly stated in this notice, no other rights or licenses, express or implied,
  30.         are granted by Apple herein, including but not limited to any patent rights that
  31.         may be infringed by your derivative works or by other works in which the Apple
  32.         Software may be incorporated.
  33.  
  34.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  35.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  36.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  38.         COMBINATION WITH YOUR PRODUCTS.
  39.  
  40.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  41.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  42.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  44.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  45.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  46.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  
  48.     Change History (most recent first):
  49.         Tue, Jan 18, 2000 -- created
  50. */
  51.  
  52.  
  53. #include "HandySample.h"
  54. #include "HandyWindow.h"
  55. #include "SampleUtils.h"
  56.  
  57. #include <Menus.h>
  58. #include <Windows.h>
  59. #include <Dialogs.h>
  60. #include <Events.h>
  61. #include <Fonts.h>
  62. #include <SegLoad.h>
  63. #include <Resources.h>
  64. #include <Balloons.h>
  65. #include <Devices.h>
  66. #include <AppleEvents.h>
  67. #include <ToolUtils.h>
  68. #include <Appearance.h>
  69. #include <Navigation.h>
  70. #include <StandardFile.h>
  71. #include <PLStringFuncs.h>
  72. #include <TextUtils.h>
  73. #include <ControlDefinitions.h>
  74. #include <Gestalt.h>
  75.  
  76.  
  77.  
  78.     /* our globals */
  79.     
  80. Boolean gRunning = true;        /* true while the app is running */
  81. RgnHandle gMouseRgn = NULL;    /* the mouse region passed to WaitNextEvent */
  82. Boolean gIsFrontApp = true;    /* true while we're in the forground. */
  83.  
  84.  
  85.  
  86. /* ResetMenus is called immediately before all calls to
  87.     MenuSelect or MenuKey.  In this routine, we re-build
  88.     or enable the menus as appropriate depending on the
  89.     current environment */
  90. static void ResetMenus(void) {
  91.     Boolean drawnSlowly;
  92.     MenuHandle fileMenu;
  93.     fileMenu = GetMenuHandle(mFile);
  94.         /* here, if the frontmost window is a handy window, then
  95.         enable and check the slow item according to it's slow drawing
  96.         state.  Otherwise, disable the item. */
  97.     if ( HandyWindowGetSlow(FrontWindow(), &drawnSlowly) == noErr ) {
  98.         EnableMenuItem(fileMenu, iSlow);
  99.         CheckMenuItem(GetMenuHandle(mFile), iSlow, drawnSlowly);
  100.     } else DisableMenuItem(fileMenu, iSlow);
  101. }
  102.  
  103.  
  104. /* DoMenuCommand is called in response to MenuKey
  105.     or MenuSelect.  Here, we dispatch the menu command
  106.     to its appropriate handler, or if it's a small action
  107.     we do it here. */
  108. static void DoMenuCommand(long rawMenuSelectResult) {
  109.     short menu, item;
  110.         /* decode the MenuSelect result */
  111.     menu = (rawMenuSelectResult >> 16);
  112.     if (menu == 0) return;
  113.     item = (rawMenuSelectResult & 0x0000FFFF);
  114.         /* dispatch on result */
  115.     switch (menu) {
  116.             /* apple menu commands */
  117.         case mApple:
  118.             if (item == iAbout) {
  119.                 ParamAlert(kAboutBoxAlertID, NULL, NULL);
  120.             }
  121.             break;
  122.             
  123.                 /* file menu commands */
  124.         case mFile:
  125.             if (item == iSlow) {
  126.                 Boolean drawnSlowly;
  127.                 WindowPtr target;
  128.                     /* if the frontmost window is a handy window, then
  129.                     invert it's slow drawing state. */
  130.                 if ( HandyWindowGetSlow((target = FrontWindow()), &drawnSlowly) == noErr ) {
  131.                     HandyWindowSetSlow(target, ! drawnSlowly);
  132.                 }
  133.             } else if (item == iQuit) {
  134.                 gRunning = false;
  135.             }
  136.             break;
  137.             
  138.     }
  139.         /* unhilite the menu bar */
  140.     HiliteMenu(0);
  141. }
  142.  
  143.  
  144. /* QuitAppleEventHandler is our quit Apple event handler.  this routine
  145.     is called when a quit Apple event is sent to our application.  Here,
  146.     we set the gRunning flag to false. NOTE:  it is not appropriate to
  147.     call ExitToShell here.  Instead, by setting the flag to false we
  148.     fall through the bottom of our main loop the next time we're called.  */
  149. static pascal OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, unsigned long refcon) {
  150.  
  151.     gRunning = false;    
  152.             
  153.     return noErr;
  154. }
  155.  
  156.  
  157.  
  158. /* HandleMouseDown is called for mouse down events.  The main difference
  159.     over 'the usual' mouse event handler, is we use the grabbing hand
  160.     cursor inside of window drag and resize operations.  Other clicks
  161.     are dispatched to routines defined in HandyWindows.h as appropriate. */
  162. static void HandleMouseDown(EventRecord *ev) {
  163.     WindowPtr theWindow;
  164.     short partcode;
  165.     switch ((partcode = FindWindow(ev->where, &theWindow))) {
  166.             /* inside the window's content area */
  167.         case inContent:
  168.             if (theWindow != FrontWindow()) {
  169.                     /* if it's not the frontmost window,
  170.                     then make it the frontmost window. */
  171.                 SelectWindow(theWindow);
  172.             } else {
  173.                     /* otherwise, if it's a rendering window,
  174.                     pass the click along to the window. */
  175.                 Point where;
  176.                 SetPort(GetWindowPort(theWindow));
  177.                 where = ev->where;
  178.                 GlobalToLocal(&where);
  179.                 if (IsHandyWindow(theWindow))
  180.                     HandyWindowMouse(theWindow, where, ev->modifiers);
  181.             }
  182.             break;
  183.             
  184.             /* menu bar clicks */
  185.         case inMenuBar:
  186.             SetThemeCursor(kThemePointingHandCursor);
  187.             ResetMenus();
  188.             DoMenuCommand(MenuSelect(ev->where));
  189.             break;
  190.             
  191.             /* track clicks in the close box */
  192.         case inGoAway:
  193.             if (TrackGoAway(theWindow, ev->where)) {
  194.                 if (IsHandyWindow(theWindow)) {
  195.                     HandyWindowCloseWindow(theWindow);
  196.                     gRunning = false;
  197.                 }
  198.             }
  199.             break;
  200.             
  201.             /* allow window drags */
  202.         case inDrag:
  203.             {    Rect boundsRect = {0,0, 32000, 32000};
  204.                 SetThemeCursor(kThemeClosedHandCursor);
  205.                 DragWindow(theWindow, ev->where, &boundsRect);
  206.             }
  207.             break;
  208.             
  209.             /* allow window drags */
  210.         case inGrow:
  211.             {    Rect sizerect;
  212.                 long grow_result;
  213.                 SetThemeCursor(kThemeClosedHandCursor);
  214.                 if (GetHandyWindowGrowLimits(theWindow, &sizerect) != noErr)
  215.                     SetRect(&sizerect, 300, 150, 32767, 32767);
  216.                 grow_result = GrowWindow(theWindow, ev->where, &sizerect);
  217.                 if (grow_result != 0) {
  218.                     SizeWindow(theWindow, LoWord(grow_result), HiWord(grow_result), true);
  219.                     if (IsHandyWindow(theWindow))
  220.                         HandyWindowSizeChanged(theWindow);
  221.                 }
  222.             }
  223.             break;
  224.             
  225.             /* zoom box clicks.  NOTE:  since the rendering window
  226.             always sets the standard rectangle to the 'best size' for
  227.             displaying the current HTML window, the inZoomOut partcode
  228.             will zoom the window to that size rather than the entire screen.*/
  229.         case inZoomIn:
  230.         case inZoomOut:
  231.             if (TrackBox(theWindow, ev->where, partcode)) {
  232.                 Rect r;
  233.                 CGrafPtr gp;
  234.                 gp = GetWindowPort(theWindow);
  235.                 GetPortBounds(gp, &r);
  236.                 SetPort(gp);
  237.                 EraseRect(&r);
  238.                 ZoomWindow(theWindow, partcode, true);
  239.                 if (IsHandyWindow(theWindow))
  240.                     HandyWindowSizeChanged(theWindow);
  241.             }
  242.             break;
  243.             
  244.     }
  245. }
  246.  
  247.  
  248. /* ResetCursor sets the cursor according to the location of the mouse on
  249.     the screen.  If the mouse is inside of a handy window, then the task of
  250.     setting the cursor is passed along to the HandyWindowCursor routine
  251.     defined in HandyWindows.c.  */
  252. static void ResetCursor(EventRecord *ev) {
  253.     WindowPtr target;
  254.     Point where;
  255.     short partcode;
  256.         /* discover the mouse's location */
  257.     switch ((partcode = FindWindow(ev->where, &target))) {
  258.             /* if it's in the content area of a window, then we either
  259.             set the cursor to an arrow (if it's not the front window) or
  260.             we ask HandyWindowCursor to set the cursor. */
  261.         case inContent:
  262.             if (target != FrontWindow()) {
  263.                 SetThemeCursor(kThemeArrowCursor);
  264.                 SetEmptyRgn(gMouseRgn);
  265.             } else {
  266.                 SetPort(GetWindowPort(target));
  267.                 where = ev->where;
  268.                 GlobalToLocal(&where);
  269.                 if ( ! HandyWindowCursor( target, where, ev->modifiers, gMouseRgn) ) {
  270.                     SetThemeCursor(kThemeArrowCursor);
  271.                     SetEmptyRgn(gMouseRgn);
  272.                 }
  273.             }
  274.             break;
  275.             
  276.             /* the following sets the cursor according to other locations
  277.             in the gui, setting the mouse region to reflect the region where
  278.             the cursor should remain as set. */
  279.         case inMenuBar:
  280.             SetThemeCursor(kThemePointingHandCursor);
  281.             GetMenuBarRegion(gMouseRgn);
  282.             break;
  283.         case inCollapseBox:
  284.             SetThemeCursor(kThemePointingHandCursor);
  285.             GetWindowRegion(target, kWindowCollapseBoxRgn, gMouseRgn);
  286.             break;
  287.         case inZoomIn:
  288.         case inZoomOut:
  289.             SetThemeCursor(kThemePointingHandCursor);
  290.             GetWindowRegion(target, kWindowZoomBoxRgn, gMouseRgn);
  291.             break;
  292.         case inGoAway:
  293.             SetThemeCursor(kThemePointingHandCursor);
  294.             GetWindowRegion(target, kWindowCloseBoxRgn, gMouseRgn);
  295.             break;
  296.         case inGrow:
  297.             SetThemeCursor(kThemeOpenHandCursor);
  298.             GetWindowRegion(target, kWindowGrowRgn, gMouseRgn);
  299.             break;
  300.         case inDrag:
  301.             SetThemeCursor(kThemeOpenHandCursor);
  302.             GetWindowRegion(target, kWindowDragRgn, gMouseRgn);
  303.             break;
  304.         default:
  305.             SetThemeCursor(kThemeArrowCursor);
  306.             SetEmptyRgn(gMouseRgn);
  307.             break;
  308.     }
  309. }
  310.  
  311.  
  312.  
  313.  
  314.  
  315. /* HandleEvent is the main event handling routine for the
  316.     application.  ev points to an event record returned by
  317.     WaitNextEvent. */
  318. void HandleEvent(EventRecord *ev) {
  319.     WindowPtr target;
  320.     
  321.         /* process other event types */
  322.     switch (ev->what) {
  323.         case keyDown:
  324.         case autoKey:
  325.             if ((ev->modifiers & cmdKey) != 0) {
  326.                 ResetMenus();
  327.                 DoMenuCommand(MenuKey((char) (ev->message & charCodeMask)));
  328.                 ev->what = nullEvent;
  329.             } else {
  330.                 target = FrontWindow();
  331.                 if (IsHandyWindow(target))
  332.                     HandyWindowKey(target, (char) (ev->message & charCodeMask), ev->modifiers);
  333.             }
  334.             break;
  335.             
  336.             /* for null events, track the cursor. */
  337.         case nullEvent:
  338.             if (gIsFrontApp)
  339.                 ResetCursor(ev);
  340.             break;
  341.  
  342.         case osEvt:
  343.             if (((ev->message >> 24) & 255) == mouseMovedMessage) {
  344.                 if (gIsFrontApp)
  345.                     ResetCursor(ev);
  346.             } else if (((ev->message >> 24) & 255) == suspendResumeMessage) {
  347.                 Boolean switchingIn;
  348.                 switchingIn = ((ev->message & resumeFlag) != 0);
  349.                     /* reset the cursor to its last known state */
  350.                 gIsFrontApp = switchingIn;
  351.                     /* send an activate event to the frontmost window */
  352.                 target = FrontWindow();
  353.                 if (IsHandyWindow(target))
  354.                     HandyWindowActivate(target, gIsFrontApp);
  355.             }
  356.             break;
  357.             
  358.             /* for activate events we call the window's activate event
  359.             handler. */
  360.         case activateEvt:
  361.             target = (WindowPtr) ev->message;
  362.             if (IsHandyWindow(target))
  363.                 HandyWindowActivate(target, ((ev->modifiers&1) != 0));
  364.             break;
  365.             
  366.             /* for update events we call the window's update event
  367.             handler. As we may be called from inside of a dialog filter
  368.             routine, if the window not a handy window then we ignore the
  369.             update event. */
  370.         case updateEvt:
  371.             target = (WindowPtr) ev->message;
  372.             if (IsHandyWindow(target))
  373.                 HandyWindowUpdate(target);
  374.             break;    
  375.             
  376.             /* for mouse events we call the the HandleMouseDown routine
  377.             defined above. */
  378.         case mouseDown:
  379.             HandleMouseDown(ev);
  380.             break;
  381.         
  382.             /* Apple events. */
  383.         case kHighLevelEvent:
  384.             AEProcessAppleEvent(ev);
  385.             break;
  386.     }
  387. }
  388.  
  389.  
  390.  
  391.  
  392. /* MyIdleInteractProc is the idle procedure called by AEInteractWithUser while we are waiting
  393.     for the application to be pulled into the forground.  It simply passes the event along
  394.     to HandleNextEvent */
  395. static pascal Boolean MyIdleInteractProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) {
  396.     HandleEvent(theEvent);
  397.     return ( ! gRunning ); /* quit waiting if we're not running */
  398. }
  399.  
  400.  
  401.  
  402.  
  403.     /* ascii codes we respond to in our filter routine */
  404. enum {
  405.     ETX = 3,        /* ENTER KEY */
  406.     CR = 13        /* RETURN KEY */
  407. };
  408.  
  409. /* MyModalFilterProc is the modal filter routine we call while alerts are
  410.     being displayed.  Its main purpose is to ensure our activation and update
  411.     routines are called appropriately for other windows displayed in our
  412.     application. */
  413. static pascal Boolean MyModalFilterProc(DialogPtr theDialog, EventRecord *theEvent, DialogItemIndex *itemHit) {
  414.     switch (theEvent->what) {
  415.         case keyDown:
  416.         case autoKey:
  417.             {    char key;
  418.                 key = (char) (theEvent->message & charCodeMask);
  419.                 if (key == ETX || key == CR) {
  420.                     *itemHit = ok;
  421.                     return true;
  422.                 }
  423.             }
  424.             break;
  425.         case nullEvent:
  426.         case osEvt:
  427.         case activateEvt:
  428.         case kHighLevelEvent:
  429.         case updateEvt:
  430.             HandleEvent(theEvent);
  431.             break;
  432.     }
  433.     return false;
  434. }
  435.  
  436.  
  437. /* ParamAlert is a general alert handling routine.  If Apple events exist, then it
  438.     calls AEInteractWithUser to ensure the application is in the forground, and then
  439.     it displays an alert after passing the s1 and s2 parameters to ParamText. */
  440. short ParamAlert(short alertID, StringPtr s1, StringPtr s2) {
  441.     AEIdleUPP aeIdleProc;
  442.     ModalFilterUPP filterProc;
  443.     OSStatus err;
  444.     filterProc = NULL;
  445.     aeIdleProc = NULL;
  446.     aeIdleProc = NewAEIdleUPP(MyIdleInteractProc);
  447.     if (aeIdleProc == NULL) { err = memFullErr; goto bail; }
  448.     filterProc = NewModalFilterUPP(MyModalFilterProc);
  449.     if (filterProc == NULL) { err = memFullErr; goto bail; }
  450.     err = AEInteractWithUser(kNoTimeOut, NULL, aeIdleProc);
  451.     if (err != noErr) goto bail;
  452.     ParamText(s1, s2, NULL, NULL);
  453.     err = Alert(alertID, filterProc);
  454.     DisposeAEIdleUPP(aeIdleProc);
  455.     DisposeModalFilterUPP(filterProc);
  456.     return err;
  457. bail:
  458.     if (aeIdleProc != NULL) DisposeAEIdleUPP(aeIdleProc);
  459.     if (filterProc != NULL) DisposeModalFilterUPP(filterProc);
  460.     return err;
  461. }
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.     /* the main program */
  471.  
  472. int main(void) {
  473.         /* allocate the mouse region.  We pass this region to
  474.         WaitNextEvent after setting the cursor so we can
  475.         receive mouse moved events. */
  476.     gMouseRgn = NewRgn();
  477.     
  478.         /* say hello to the appearance manager */
  479.     RegisterAppearanceClient();
  480.     
  481.         /* set the cursor to a handy one... */
  482.     SetThemeCursor(kThemePointingHandCursor);
  483.     
  484.         /* install our event handlers */
  485.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(QuitAppleEventHandler), 0, false);
  486.  
  487.         /* set up the menu bar */
  488.     SetMenuBar(GetNewMBar(kMacOS9MenuBarID));
  489.     DrawMenuBar();
  490.     
  491.         /* open some handy windows */
  492.     OpenHandyWindow(kHandyWindowTwo, true);
  493.     OpenHandyWindow(kHandyWindowOne, false);
  494.     
  495.         /* run the app */
  496.     while (gRunning) {
  497.         EventRecord ev;
  498.         RgnHandle theMouseRgn;
  499.             /* if the mouse region is empty, then we'll pass NULL to WaitNextEvent. */
  500.         if (EmptyRgn(gMouseRgn)) theMouseRgn = NULL; else theMouseRgn = gMouseRgn;
  501.             /* get the next event */
  502.         if ( ! WaitNextEvent(everyEvent, &ev,  GetCaretTime(), theMouseRgn) )
  503.             ev.what = nullEvent;
  504.             /* call our handler to deal with it. */
  505.         HandleEvent(&ev);
  506.  
  507.     }
  508.     
  509.         /* close all of our windows. */
  510.     CloseAllHandyWindows();
  511.         /* close down and leave. */
  512.     UnregisterAppearanceClient();
  513.     DisposeRgn(gMouseRgn);
  514.     ExitToShell();
  515.     return 0;
  516. }